package com.traffic.api.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayMobilePublicMessageMatcherSendRequest;
import com.alipay.api.response.AlipayMobilePublicMessageMatcherSendResponse;
import com.traffic.api.bean.FollowServiceWindowBizContent;
import com.traffic.api.bean.FollowServiceWindowTemplate;
import com.traffic.api.bean.FollowServiceWindowTemplateContext;
import com.traffic.api.util.AlipayUtils;
import com.traffic.api.util.BaseResponse;
import com.traffic.api.util.ResultCodeAndMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.traffic.api.util.GlobalUrl.*;

/**
 * Created by churcy on 2017/3/17.
 */
@Controller
public class AlipayServiceWindowController {


    /**
     * 匹配关注生活号
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/follow/service/window", method = RequestMethod.POST)
    public Object followServiceWindow(HttpServletRequest request) {
        Map<String, Object> obj = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        boolean isException = true;
        String templateId = request.getParameter("templateId");
        String userId = request.getParameter("userId");
        String headColor = request.getParameter("headColor");
        String remarkColor = request.getParameter("remarkColor");
        String remarkValue = request.getParameter("remarkValue");
        String firstColor = request.getParameter("firstColor");
        String firstValue = request.getParameter("firstValue");
        String url = request.getParameter("url");
        if(StringUtils.isBlank(url)){
            url = ALIPAY_BUS_PAY_QC;
        }
        String key1Color = request.getParameter("key1Color");
        String key1Value = request.getParameter("key1Value");
        String key2Color = request.getParameter("key2Color");
        String key2Value = request.getParameter("key2Value");
        String key3Color = request.getParameter("key3Color");
        String key3Value = request.getParameter("key3Value");
        String key4Color = request.getParameter("key4Color");
        String key4Value = request.getParameter("key4Value");
        String key5Color = request.getParameter("key5Color");
        String key5Value = request.getParameter("key5Value");
        try {
            do {
                AlipayClient alipayClient =
                        new DefaultAlipayClient(ALIPAY_GATEWAY, SERVER_APP_ID, SERVER_APP_PRIVATE_KEY, "json", UTF_8, SERVER_ALIPAY_PUBLIC_KEY, SIGN_TYPE_RSA);
                AlipayMobilePublicMessageMatcherSendRequest matcherSendRequest = new AlipayMobilePublicMessageMatcherSendRequest();
                String bizContent = formatFollowServiceWindowBizContent(templateId, userId, headColor, url,
                        firstColor, firstValue, remarkColor, remarkValue, key1Color, key1Value, key2Color,
                        key2Value, key3Color, key3Value, key4Color, key4Value, key5Color, key5Value);
                matcherSendRequest.setBizContent(bizContent);
                System.out.println(bizContent);
                AlipayMobilePublicMessageMatcherSendResponse response = alipayClient.execute(matcherSendRequest);
                result = AlipayUtils.checkAlipayResponse(response);
                obj = (Map<String, Object>) result.get("obj");
                isException = (boolean) result.get("flag");//是否存在异常标记
                if (isException) {
                    break;
                }
                obj = BaseResponse.setResponse(null, ResultCodeAndMessage.SUCCESS);
            } while (false);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return obj;

    }

    /**
     * 格式化bizContent
     *
     * @param templateId
     * @param userId
     * @param headColor
     * @param url
     * @param firstColor
     * @param firstValue
     * @param remarkColor
     * @param remarkValue
     * @param key1Color
     * @param key1Value
     * @param key2Color
     * @param key2Value
     * @param key3Color
     * @param key3Value
     * @param key4Color
     * @param key4Value
     * @param key5Color
     * @param key5Value
     * @return
     */
    public String formatFollowServiceWindowBizContent(String templateId, String userId, String headColor, String url,
                                                      String firstColor, String firstValue, String remarkColor, String remarkValue,
                                                      String key1Color, String key1Value,
                                                      String key2Color, String key2Value, String key3Color, String key3Value,
                                                      String key4Color, String key4Value, String key5Color, String key5Value) {
        FollowServiceWindowBizContent bizContent = new FollowServiceWindowBizContent();
        Map<String, Object> matcher = new HashMap<>();
        matcher.put("userId", userId);
        bizContent.setMatcher(matcher);
        FollowServiceWindowTemplate template = new FollowServiceWindowTemplate();
        Map<String, Object> first = new HashMap<>();
        first.put("color", firstColor);
        first.put("value", firstValue);
        Map<String, Object> remark = new HashMap<>();
        remark.put("color", remarkColor);
        remark.put("value", remarkValue);
        Map<String, Object> keyword1 = new HashMap<>();
        keyword1.put("color", key1Color);
        keyword1.put("value", key1Value);
        Map<String, Object> keyword2 = new HashMap<>();
        keyword2.put("color", key2Color);
        keyword2.put("value", key2Value);
        Map<String, Object> keyword3 = new HashMap<>();
        keyword3.put("color", key3Color);
        keyword3.put("value", key3Value);
        Map<String, Object> keyword4 = new HashMap<>();
        keyword4.put("color", key4Color);
        keyword4.put("value", key4Value);
        Map<String, Object> keyword5 = new HashMap<>();
        keyword5.put("color", key5Color);
        keyword5.put("value", key5Value);
        template.setTemplateId(templateId);
        /*FollowServiceWindowTemplateContext context = new FollowServiceWindowTemplateContext.ContextBuilder()
                .setFirst(first).setHeadColor(headColor).setUrl(url).setRemark(remark)
                .setKeyword1(keyword1).setKeyword2(keyword2).setKeyword3(keyword3).setKeyword4(keyword4)
                .setKeyword5(keyword5).build();*/
        FollowServiceWindowTemplateContext context = new FollowServiceWindowTemplateContext();
        context.setRemark(remark);
        context.setFirst(first);
        context.setHeadColor(headColor);
        context.setKeyword1(keyword1);
        context.setKeyword2(keyword2);
        context.setUrl(url);
        String contextStr  = JSON.toJSONString(context);
        template.setContext(context);
        bizContent.setTemplate(template);
        String response = JSON.toJSONString(bizContent);
        return response;
    }
}
